package com.codingvip.queue;

public class MyCircularQueue {

    Object[] datas;
    int front;
    int count;
    int size;

    /**
     * Initialize your data structure here. Set the size of the queue to be k.
     */
    public MyCircularQueue(int k) {
        datas = new Object[k];
        this.size = k;
        this.front = 0;
        this.count = 0;
    }

    /**
     * Insert an element into the circular queue. Return true if the operation is successful.
     */
    public boolean enQueue(int value) {
        if (isFull()) {
            return false;
        }
        datas[(front + count)%size] = value;
        count++;
        return true;
    }

    /**
     * Delete an element from the circular queue. Return true if the operation is successful.
     */
    public boolean deQueue() {
        if (isEmpty()) {
            return false;
        }
        datas[front] = null;
        front = (front + 1) % size;
        count--;
        return true;
    }

    /**
     * Get the front item from the queue.
     */
    public int Front() {
        if (isEmpty()) {
            return -1;
        }
        return (int) datas[front];
    }

    /**
     * Get the last item from the queue.
     */
    public int Rear() {
        if (isEmpty()) {
            return -1;
        }
        return (int) datas[(front + count - 1) % size];
    }


    /**
     * Checks whether the circular queue is empty or not.
     */
    public boolean isEmpty() {
        if (count == 0) {
            return true;
        }
        return false;
    }

    /**
     * Checks whether the circular queue is full or not.
     */
    public boolean isFull() {
        if (count == size) {
            return true;
        }
        return false;
    }

    /**
     * Your MyCircularQueue object will be instantiated and called as such:
     * MyCircularQueue obj = new MyCircularQueue(k);
     * boolean param_1 = obj.enQueue(value);
     * boolean param_2 = obj.deQueue();
     * int param_3 = obj.Front();
     * int param_4 = obj.Rear();
     * boolean param_5 = obj.isEmpty();
     * boolean param_6 = obj.isFull();
     * ["MyCircularQueue","enQueue","enQueue","enQueue","enQueue","Rear","isFull","deQueue","enQueue","Rear"]
     * [[3],[1],[2],[3],[4],[],[],[],[4],[]]
     */

    public static void main(String[] args) {
        MyCircularQueue obj = new MyCircularQueue(3);
        boolean param_1 = obj.enQueue(1);
        boolean param_6 = obj.enQueue(2);
        boolean param_7 = obj.enQueue(3);
        boolean param_8 = obj.enQueue(4);
        int param_2 = obj.Rear();
        boolean param_11 = obj.isFull();
        boolean parm_12 = obj.deQueue();
        boolean param_13 = obj.enQueue(4);
        int param_4 = obj.Rear();


    }

}

